package bishi.Huawei;

import java.util.Scanner;

/**
 * @Classname Main1
 * @Description TODO
 * @Date 2021/9/1 20:26
 * @Created by ASUS
 */
public class Main1 {
    /*
    2
50,60 30,25
120
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        int[][] nodes = new int[num][2];
        String[] strs = scanner.nextLine().split(" ");
        for (int i = 0; i < num; i++) {
            String[] split = strs[i].split(",");
            nodes[i][0] = Integer.parseInt(split[0]);
            nodes[i][1] = Integer.parseInt(split[1]);
        }
        int data_pkg = Integer.parseInt(scanner.nextLine());
        scanner.close();
        int r1csend = data_pkg, r2csend = 0, r1ccached = 0, r1dsend = data_pkg, r2dsend = 0, r1dcached = 0;
        for (int i = 0; i < num; i++) {
            int qr1csend = 0, qr2csend = 0, qr1ccached = 0, qr1dsend = r1csend, qr1dcached = r1ccached, qr2dsend = r2csend;
            qr1csend = Math.min(r1dsend, nodes[i][0]);
            qr1ccached = Math.min(nodes[i][1], r1csend);
            qr2csend = Math.min(qr1ccached + qr2dsend, nodes[i][0]);
            int qqr1csend = Math.min(r1csend, nodes[i][0]);
            int qqr1ccached = Math.min(nodes[i][1], r1csend - qqr1csend);
            int qqr2csend = Math.min(qqr1ccached + r2csend, nodes[i][0]);
            r1csend = Math.min(qr1csend, qqr1csend);
            r1ccached = Math.max(qr1ccached, qqr1ccached);
            r2csend = Math.min(qr2csend, qqr2csend);
            r1dsend = qr1dsend;
            r1dcached = qr1dcached;
            r2dsend = qr2dsend;
        }
        System.out.println(Math.min(r1csend + r2csend, r1dsend + r2dsend));
    }
}
